// pages/order/order.js
import {
    request
} from "../../request/index.js";
Page({

    /**
     * 页面的初始数据
     */
    data: {
        collect: [],
        tabs: [{
                id: 0,
                value: "已完成",
                isActive: true
            },
            {
                id: 1,
                value: "待送达",
                isActive: false
            },
            {
                id: 2,
                value: "已退款",
                isActive: false
            }
        ]
    },
    onLoad: function(options) {
        this.getOrderList();
    },
    onShow() {
        const collect = wx.getStorageSync("collect") || [];
        //添加是否展示属性
        collect.forEach(v => v["isShow"] = false);
        this.setData({
            collect
        });
    },
 
    //删除记录,长按删除
    deletedata(e){
       wx.showModal({
         cancelColor: 'red',
         title: '提示',
         content: '确定要删除吗？',
         success:(sm) =>{
            if (sm.confirm) {
                const { index } = e.currentTarget.dataset;
                console.log(index);
                let {collect} =this.data;
                collect.splice(index, 1);
                this.setData({
                  collect
            })
            } else if (sm.cancel) {
                console.log('用户点击取消')
            }
        }
       })
    },

    //标题的点击事件，不过此事件是从子组件中传递的
    handletabsItemChange(e) {
        //1 获取被点击的标题索引
        const { index } = e.detail;
        // 2 修改数组
        let { tabs } = this.data;
        tabs.forEach((v, i) => i === index ? v.isActive = true : v.isActive = false);
        //3 赋值到data中
        this.setData({
            tabs
        })
    },
    //展开事件
    handleItemShow(e) {
        const { collect } = this.data;
        let { index } = e.currentTarget.dataset;
        collect[index].isShow = !collect[index].isShow;
        this.setData({
            collect
        })

    },
    //获取订单信息
    async getOrderList(){
        let res = await request({
            url: "my/order/getOrder",
            dataType: 'json',
            responseType: 'text',
            header: {
                'content-type': 'application/x-www-form-urlencoded',
                'Authorization': wx.getStorageSync('token')
            },
            method: 'GET',
        });
        // orderList = JSON.parse(orderList);
        this.setData({
            collect:res.orderList
        })
        wx.setStorageSync('collect', res.orderList);
        console.log(res);
    },
   

    gotopage:function(options){
       wx.navigateTo({
        url: '../comments/comments',
       })
    },
    gotorefund:function(option){
        const goods_id=option.currentTarget.dataset.goods_id;
        wx.navigateTo({
          url: '../refund/refund?goods_id='+goods_id,
        })
    }
})